package com.study.spmongodb.book.wwj.char04;

import java.util.concurrent.TimeUnit;

/**
 * @author MI
 * @version 1.0
 * @date 2020/11/24 20:24
 */
public class Mutex {
    private final static Object MUTEX = new Object();

    public void accessResource() {
        synchronized (MUTEX) {
            try {
                TimeUnit.MINUTES.sleep(10);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        final Mutex mutex = new Mutex();
        for (int i = 0; i < 5; i++) {
            //由于同步代码块的互斥性，只能有一个线程获取了 mutex monitor锁，其他线程只能进入阻塞状态。
            new Thread(mutex::accessResource).start();
        }
    }
}
